/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue.basic.server.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.feiyue.basic.server.parser.Category;
import java.util.ArrayList;
import java.util.List;

public class CategoryDBHandler {
	public static final String COLUMN_coveraddr = "coveraddr";
	public static final String COLUMN_id = "id";
	public static final String COLUMN_name = "name";
	public static final String CREATE_TABLE_categorylist = "CREATE TABLE IF NOT EXISTS categorylist (id INTEGER PRIMARY KEY not null, name text not null, coveraddr text not null);";
	public static final String myTableName = "categorylist";
	protected SQLiteDatabase mSQLiteDatabase = null;

	public CategoryDBHandler(Context paramContext) throws Exception {
		this.mSQLiteDatabase = paramContext.openOrCreateDatabase("novel", 0,
				null);
		this.mSQLiteDatabase
				.execSQL("CREATE TABLE IF NOT EXISTS categorylist (id INTEGER PRIMARY KEY not null, name text not null, coveraddr text not null);");
	}

	private void insert(Category paramCategory, Context paramContext) {
		if (isExist(paramCategory))
			return;
		String str = "insert into categorylist values(" + paramCategory.getId()
				+ ", \"" + paramCategory.getName() + "\", \""
				+ paramCategory.getCoverAddr() + "\"" + ");";
		this.mSQLiteDatabase.execSQL(str);
	}

	private boolean isExist(Category paramCategory) {
		String str = "select * from categorylist where id = "
				+ paramCategory.getId();
		Cursor localCursor = null;
		try {
			localCursor = this.mSQLiteDatabase.rawQuery(str, null);
			if (localCursor != null && localCursor.moveToFirst()) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (localCursor != null) {
				localCursor.close();
			}
		}
		return false;
	}

	public void destroy() {
		if (this.mSQLiteDatabase == null)
			return;
		this.mSQLiteDatabase.close();
	}

	public List<Category> getCategoryList(Context paramContext) {
		ArrayList localArrayList = new ArrayList();
		Cursor localCursor = null;
		try {
			localCursor = this.mSQLiteDatabase.rawQuery(
					"select * from categorylist", null);
			if (localCursor != null && localCursor.moveToFirst())
				do {
					Category localCategory = new Category();
					String str1 = localCursor.getString(localCursor
							.getColumnIndex("coveraddr"));
					String str2 = localCursor.getString(localCursor
							.getColumnIndex("name"));
					int i = localCursor
							.getInt(localCursor.getColumnIndex("id"));
					localCategory.setCoverAddr(str1);
					localCategory.setName(str2);
					localCategory.setId(i);
					localArrayList.add(localCategory);
				} while (localCursor.moveToNext());
			
			return localArrayList;
		} catch (Exception localException) {
			localException.printStackTrace();
		} finally {
			if (localCursor != null) {
				localCursor.close();
			}
		}
		return null;
	}

	public void insert(List<Category> paramList, Context paramContext) {
		if (paramList == null)
			return;
		int i = paramList.size();
		for (int j = 0;; ++j) {
			if (j < i) {
				insert((Category) paramList.get(j), paramContext);
			}
		}
	}
}